//
//  FAddNewCodeTool.m
//  WaWaSchool
//
//  Created by fls on 2019/7/18.
//  Copyright © 2019年 Galaxy School. All rights reserved.
//

#import "FAddNewCodeTool.h"

@implementation FAddNewCodeTool
/**
 返回富文本
 
 @param string 文字
 @param stringColor 所有文字的颜色
 @param allFont 所有文字的font
 @param otherStrColor 部分文字的颜色
 @param otherString 部分文字
 @param otherFont 部分文字的font
 @return 返回富文本
 */
+ (NSMutableAttributedString *)backStringAttributedString:(NSString *)string
                                           allStringColor:(UIColor *)stringColor
                                            allStringFont:(CGFloat)allFont
                                            OtherStrColor:(UIColor *)otherStrColor
                                                 OtherStr:(NSString *)otherString
                                             otherStrFont:(CGFloat)otherFont{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    //    所有文字
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:allFont] range:NSMakeRange(0, string.length)];//字体大小
    [attributedString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];//字体颜色
    
    NSRange otherStrRange = [string rangeOfString:otherString];
    
    
    //    部分文字
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:otherFont] range:otherStrRange];//字体大小
    [attributedString addAttribute:NSForegroundColorAttributeName value:otherStrColor range:otherStrRange];//字体颜色
    return attributedString;
}

/**
 返回富文本

 @param string 文字
 @param stringColor 所有文字的颜色
 @param allFont 所有文字的font
 @param otherStrColor 部分文字的颜色
 @param otherRange 部分文字range
 @param otherFont 部分文字的font
 @param imageName 图片名称
 @param imageInsertIndex 图片插入的位置
 @param imageBounds 图片大小
 @return 返回富文本
 */
+ (NSMutableAttributedString *)backAttributedString:(NSString *)string
                                     allStringColor:(UIColor *)stringColor
                                      allStringFont:(CGFloat)allFont
                                      OtherStrColor:(UIColor *)otherStrColor
                                      OtherStrRange:(NSRange)otherRange
                                       otherStrFont:(CGFloat)otherFont
                                          imageName:(NSString *)imageName
                                   insertImageIndex:(NSUInteger)imageInsertIndex
                                        imageBounds:(CGRect)imageBounds{
   
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
//    所有文字
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:allFont] range:NSMakeRange(0, string.length)];//字体大小
    [attributedString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];//字体颜色
//    部分文字
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:otherFont] range:otherRange];//字体大小
    [attributedString addAttribute:NSForegroundColorAttributeName value:otherStrColor range:otherRange];//字体颜色

//    插入图片
    if (imageName != nil) {
        NSTextAttachment * attchment = [[NSTextAttachment alloc]init];
        attchment.bounds = imageBounds;//设置frame
        attchment.image = [UIImage imageNamed:imageName];//设置图片
        NSAttributedString * imageString = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];    [attributedString insertAttributedString:imageString atIndex:imageInsertIndex];//插入到第几个下标
    }

    return attributedString;
}

/**
 返回富文本

 @param string 文字
 @param stringColor 文字颜色
 @param strFont 文字大小
 @param imageName 图片名称
 @param imageBounds 图片大小
 @param index 图片插入位置
 @return 返回富文本
 */
+ (NSMutableAttributedString *)backAttributedString:(NSString *)string
                                        stringColor:(UIColor *)stringColor
                                         stringFont:(CGFloat)strFont
                                              image:(NSString *)imageName
                                        imageBounds:(CGRect)imageBounds
                                   imageInsertIndex:(NSUInteger)index{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    //    所有文字
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:strFont] range:NSMakeRange(0, string.length)];//字体大小
    [attributedString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(0, string.length)];//字体颜色
    //    插入图片
    if (imageName != nil) {
        NSTextAttachment * attchment = [[NSTextAttachment alloc]init];
        attchment.bounds = imageBounds;//设置frame
        attchment.image = [UIImage imageNamed:imageName];//设置图片
        NSAttributedString * imageString = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];    [attributedString insertAttributedString:imageString atIndex:index];//插入到第几个下标
    }
    
    return attributedString;
    
}
/**
 修改字符串中数字颜色, 并返回对应富文本
 
 @param color 数字颜色, 包括小数
 @param normalColor 默认颜色
 @return 结果富文本
 */
+ (NSMutableAttributedString *)modifyDigitalColor:(UIColor *)color normalColor:(UIColor *)normalColor string:(NSString *) string font:(CGFloat)font
{
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"([0-9]\\d*\\.?\\d*)" options:0 error:NULL];
    
    NSArray<NSTextCheckingResult *> *ranges = [regular matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSForegroundColorAttributeName : normalColor}];//字体颜色
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, string.length)];//字体大小
    
    for (int i = 0; i < ranges.count; i++) {
        [attStr setAttributes:@{NSForegroundColorAttributeName : color} range:ranges[i].range];
    }
    return attStr;
}
/**
 修改字符串中数字颜色, 并返回对应富文本
 
 @param color 数字颜色, 包括小数
 @param digitalfont 数字大小
 @param normalColor 默认颜色
 @return 结果富文本
 */
+ (NSMutableAttributedString *)modifyDigitalColor:(UIColor *)color Digitalfont:(CGFloat)digitalfont normalColor:(UIColor *)normalColor string:(NSString *) string font:(CGFloat)font
{
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"([0-9]\\d*\\.?\\d*)" options:0 error:NULL];
    
    NSArray<NSTextCheckingResult *> *ranges = [regular matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSForegroundColorAttributeName : normalColor}];//字体颜色
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, string.length)];//字体大小
    
    for (int i = 0; i < ranges.count; i++) {
        [attStr setAttributes:@{NSForegroundColorAttributeName : color} range:ranges[i].range];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:digitalfont] range:ranges[i].range];
    }
    return attStr;
}
+ (BOOL)deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    if ([str isEqualToString:@"0"]) {
        return NO;
    }
    if ([str intValue] <= 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}
/**
 把空字符串或者<null>之类的转为@""
 
 @param object object
 @return string
 */
+(NSString *)transToStringWithObject:(id)object {
    NSString *string = [NSString stringWithFormat:@"%@",object];
    NSString * subString = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([subString isEqualToString:@"<null>"] || [subString isEqualToString:@"<NULL>"] || subString == nil ||subString.length == 0) {
        return @"";
    }else {
        return string;
    }
}
@end
